package com.j.lemon.learn.clickhouse;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CKCreateTable {
    public static void main(String[] args) throws SQLException {
        String driverClassName = "ru.yandex.clickhouse.ClickHouseDriver";
        String userName = "audit4a";
        String password = "rryzggTR";
        String [] urls = new String[]{
                "jdbc:clickhouse://10.28.149.247:8123/default",
                "jdbc:clickhouse://10.28.149.248:8123/default",
                "jdbc:clickhouse://10.28.149.249:8123/default",
                "jdbc:clickhouse://10.28.149.247:28123/default",
                "jdbc:clickhouse://10.28.149.248:28123/default",
                "jdbc:clickhouse://10.28.149.249:28123/default"
        };

        String [] distributeUrl = new String[]{
                "jdbc:clickhouse://10.28.149.247:28123/default",
                "jdbc:clickhouse://10.28.149.248:28123/default",
                "jdbc:clickhouse://10.28.149.249:28123/default"
        };

        String localTableSql = "";
        String distributeTableSql = "create table behavior_log_pri2_test_all\n" +
                "(\n" +
                "    userid             Int32,\n" +
                "    btag               String,\n" +
                "    cate               String,\n" +
                "    brand              String,\n" +
                "    opt_time           DateTime,\n" +
                "    ext1               String,\n" +
                "    ext2               String,\n" +
                "    ext3               String,\n" +
                "    ext4               String,\n" +
                "    ext5               String,\n" +
                "    ext6               String,\n" +
                "    ext7               String,\n" +
                "    ext8               String,\n" +
                "    ext9               String,\n" +
                "    ext10              String,\n" +
                "    ext11              String,\n" +
                "    ext12              String,\n" +
                "    ext13              String,\n" +
                "    ext14              String,\n" +
                "    ext15              String,\n" +
                "    ext16              String,\n" +
                "    ext17              String,\n" +
                "    ext18              String,\n" +
                "    ext19              String,\n" +
                "    ext20              String,\n" +
                "    ext21              String,\n" +
                "    ext22              String,\n" +
                "    ext23              String,\n" +
                "    ext24              String,\n" +
                "    ext25              String,\n" +
                "    ext26              String,\n" +
                "    ext27              String,\n" +
                "    ext28              String,\n" +
                "    ext29              String,\n" +
                "    ext30              String,\n" +
                "    ext31              String,\n" +
                "    ext32              String,\n" +
                "    ext33              String,\n" +
                "    ext34              String,\n" +
                "    ext35              String,\n" +
                "    ext36              String,\n" +
                "    ext37              String,\n" +
                "    ext38              String,\n" +
                "    ext39              String,\n" +
                "    ext40              String,\n" +
                "    ext41              String,\n" +
                "    ext42              String,\n" +
                "    ext43              String,\n" +
                "    ext44              String,\n" +
                "    ext45              String,\n" +
                "    ext46              String,\n" +
                "    ext47              String,\n" +
                "    ext48              String,\n" +
                "    ext49              String,\n" +
                "    ext50              String,\n" +
                "    ext51              String,\n" +
                "    ext52              String,\n" +
                "    ext53              String,\n" +
                "    ext54              String,\n" +
                "    ext55              String,\n" +
                "    ext56              String,\n" +
                "    ext57              String,\n" +
                "    ext58              String,\n" +
                "    ext59              String,\n" +
                "    ext60              String,\n" +
                "    ext61              String,\n" +
                "    ext62              String,\n" +
                "    ext63              String,\n" +
                "    ext64              String,\n" +
                "    ext65              String,\n" +
                "    ext66              String,\n" +
                "    ext67              String,\n" +
                "    ext68              String,\n" +
                "    ext69              String,\n" +
                "    ext70              String,\n" +
                "    ext71              String,\n" +
                "    ext72              String,\n" +
                "    ext73              String,\n" +
                "    ext74              String,\n" +
                "    ext75              String,\n" +
                "    ext76              String,\n" +
                "    ext77              String,\n" +
                "    ext78              String,\n" +
                "    ext79              String,\n" +
                "    ext80              String,\n" +
                "    ext81              String,\n" +
                "    ext82              String,\n" +
                "    ext83              String,\n" +
                "    ext84              String,\n" +
                "    ext85              String,\n" +
                "    ext86              String,\n" +
                "    ext87              String,\n" +
                "    ext88              String,\n" +
                "    ext89              String,\n" +
                "    ext90              String,\n" +
                "    ext91              String,\n" +
                "    ext92              String,\n" +
                "    ext93              String,\n" +
                "    ext94              String,\n" +
                "    ext95              String,\n" +
                "    ext96              String,\n" +
                "    ext97              String,\n" +
                "    ext98              String,\n" +
                "    ext99              String,\n" +
                "    ext100             String,\n" +
                "    ext101             String,\n" +
                "    ext102             String,\n" +
                "    ext103             String,\n" +
                "    ext104             String,\n" +
                "    ext105             String,\n" +
                "    ext106             String,\n" +
                "    ext107             String,\n" +
                "    ext108             String,\n" +
                "    ext109             String,\n" +
                "    ext110             String,\n" +
                "    ext111             String,\n" +
                "    ext112             String,\n" +
                "    ext113             String,\n" +
                "    ext114             String,\n" +
                "    ext115             String,\n" +
                "    ext116             String,\n" +
                "    ext117             String,\n" +
                "    ext118             String,\n" +
                "    ext119             String,\n" +
                "    ext120             String,\n" +
                "    ext121             String,\n" +
                "    ext122             String,\n" +
                "    ext123             String,\n" +
                "    ext124             String,\n" +
                "    ext125             String,\n" +
                "    ext126             String,\n" +
                "    ext127             String,\n" +
                "    ext128             String,\n" +
                "    ext129             String,\n" +
                "    ext130             String,\n" +
                "    ext131             String,\n" +
                "    ext132             String,\n" +
                "    ext133             String,\n" +
                "    ext134             String,\n" +
                "    ext135             String,\n" +
                "    ext136             String,\n" +
                "    ext137             String,\n" +
                "    ext138             String,\n" +
                "    ext139             String,\n" +
                "    ext140             String,\n" +
                "    ext141             String,\n" +
                "    ext142             String,\n" +
                "    ext143             String,\n" +
                "    ext144             String,\n" +
                "    ext145             String,\n" +
                "    ext146             String,\n" +
                "    ext147             String,\n" +
                "    ext148             String,\n" +
                "    ext149             String,\n" +
                "    ext150             String,\n" +
                "    ext151             String,\n" +
                "    ext152             String,\n" +
                "    ext153             String,\n" +
                "    ext154             String,\n" +
                "    ext155             String,\n" +
                "    ext156             String,\n" +
                "    ext157             String,\n" +
                "    ext158             String,\n" +
                "    ext159             String,\n" +
                "    ext160             String,\n" +
                "    ext161             String,\n" +
                "    ext162             String,\n" +
                "    ext163             String,\n" +
                "    ext164             String,\n" +
                "    ext165             String,\n" +
                "    ext166             String,\n" +
                "    ext167             String,\n" +
                "    ext168             String,\n" +
                "    ext169             String,\n" +
                "    ext170             String,\n" +
                "    ext171             String,\n" +
                "    ext172             String,\n" +
                "    ext173             String,\n" +
                "    ext174             String,\n" +
                "    ext175             String,\n" +
                "    ext176             String,\n" +
                "    ext177             String,\n" +
                "    ext178             String,\n" +
                "    ext179             String,\n" +
                "    ext180             String,\n" +
                "    ext181             String,\n" +
                "    ext182             String,\n" +
                "    ext183             String,\n" +
                "    ext184             String,\n" +
                "    ext185             String,\n" +
                "    ext186             String,\n" +
                "    ext187             String,\n" +
                "    ext188             String,\n" +
                "    ext189             String,\n" +
                "    ext190             String,\n" +
                "    ext191             String,\n" +
                "    ext192             String,\n" +
                "    ext193             String,\n" +
                "    ext194             String,\n" +
                "    ext195             String,\n" +
                "    ext196             String,\n" +
                "    ext197             String,\n" +
                "    ext198             String,\n" +
                "    ext199             String,\n" +
                "    ext200             String,\n" +
                "    ext201             String,\n" +
                "    ext202             String,\n" +
                "    ext203             String,\n" +
                "    ext204             String,\n" +
                "    ext205             String,\n" +
                "    ext206             String,\n" +
                "    ext207             String,\n" +
                "    ext208             String,\n" +
                "    ext209             String,\n" +
                "    ext210             String,\n" +
                "    ext211             String,\n" +
                "    ext212             String,\n" +
                "    ext213             String,\n" +
                "    ext214             String,\n" +
                "    ext215             String,\n" +
                "    ext216             String,\n" +
                "    ext217             String,\n" +
                "    ext218             String,\n" +
                "    ext219             String,\n" +
                "    ext220             String,\n" +
                "    ext221             String,\n" +
                "    ext222             String,\n" +
                "    ext223             String,\n" +
                "    ext224             String,\n" +
                "    ext225             String,\n" +
                "    ext226             String,\n" +
                "    ext227             String,\n" +
                "    ext228             String,\n" +
                "    ext229             String,\n" +
                "    ext230             String,\n" +
                "    ext231             String,\n" +
                "    ext232             String,\n" +
                "    ext233             String,\n" +
                "    ext234             String,\n" +
                "    ext235             String,\n" +
                "    ext236             String,\n" +
                "    ext237             String,\n" +
                "    ext238             String,\n" +
                "    ext239             String,\n" +
                "    ext240             String,\n" +
                "    ext241             String,\n" +
                "    ext242             String,\n" +
                "    ext243             String,\n" +
                "    ext244             String,\n" +
                "    ext245             String,\n" +
                "    ext246             String,\n" +
                "    ext247             String,\n" +
                "    ext248             String,\n" +
                "    ext249             String,\n" +
                "    ext250             String,\n" +
                "    ext251             String,\n" +
                "    ext252             String,\n" +
                "    ext253             String,\n" +
                "    ext254             String,\n" +
                "    ext255             String,\n" +
                "    ext256             String,\n" +
                "    ext257             String,\n" +
                "    ext258             String,\n" +
                "    ext259             String,\n" +
                "    ext260             String,\n" +
                "    ext261             String,\n" +
                "    ext262             String,\n" +
                "    ext263             String,\n" +
                "    ext264             String,\n" +
                "    ext265             String,\n" +
                "    ext266             String,\n" +
                "    ext267             String,\n" +
                "    ext268             String,\n" +
                "    ext269             String,\n" +
                "    ext270             String,\n" +
                "    ext271             String,\n" +
                "    ext272             String,\n" +
                "    ext273             String,\n" +
                "    ext274             String,\n" +
                "    ext275             String,\n" +
                "    ext276             String,\n" +
                "    ext277             String,\n" +
                "    ext278             String,\n" +
                "    ext279             String,\n" +
                "    ext280             String,\n" +
                "    ext281             String,\n" +
                "    ext282             String,\n" +
                "    ext283             String,\n" +
                "    ext284             String,\n" +
                "    ext285             String,\n" +
                "    ext286             String,\n" +
                "    ext287             String,\n" +
                "    ext288             String,\n" +
                "    ext289             String,\n" +
                "    ext290             String,\n" +
                "    ext291             String,\n" +
                "    ext292             String,\n" +
                "    ext293             String,\n" +
                "    ext294             String,\n" +
                "    ext295             String,\n" +
                "    ext296             String,\n" +
                "    ext297             String,\n" +
                "    ext298             String,\n" +
                "    ext299             String,\n" +
                "    ext300             String,\n" +
                "    ext301             String,\n" +
                "    ext302             String,\n" +
                "    ext303             String,\n" +
                "    ext304             String,\n" +
                "    ext305             String,\n" +
                "    ext306             String,\n" +
                "    ext307             String,\n" +
                "    ext308             String,\n" +
                "    ext309             String,\n" +
                "    ext310             String,\n" +
                "    ext311             String,\n" +
                "    ext312             String,\n" +
                "    ext313             String,\n" +
                "    ext314             String,\n" +
                "    ext315             String,\n" +
                "    ext316             String,\n" +
                "    ext317             String,\n" +
                "    ext318             String,\n" +
                "    ext319             String,\n" +
                "    ext320             String,\n" +
                "    ext321             String,\n" +
                "    ext322             String,\n" +
                "    ext323             String,\n" +
                "    ext324             String,\n" +
                "    ext325             String,\n" +
                "    ext326             String,\n" +
                "    ext327             String,\n" +
                "    ext328             String,\n" +
                "    ext329             String,\n" +
                "    ext330             String,\n" +
                "    ext331             String,\n" +
                "    ext332             String,\n" +
                "    ext333             String,\n" +
                "    ext334             String,\n" +
                "    ext335             String,\n" +
                "    ext336             String,\n" +
                "    ext337             String,\n" +
                "    ext338             String,\n" +
                "    ext339             String,\n" +
                "    ext340             String,\n" +
                "    ext341             String,\n" +
                "    ext342             String,\n" +
                "    ext343             String,\n" +
                "    ext344             String,\n" +
                "    ext345             String,\n" +
                "    ext346             String,\n" +
                "    ext347             String,\n" +
                "    ext348             String,\n" +
                "    ext349             String,\n" +
                "    ext350             String,\n" +
                "    ext351             String,\n" +
                "    ext352             String,\n" +
                "    ext353             String,\n" +
                "    ext354             String,\n" +
                "    ext355             String,\n" +
                "    ext356             String,\n" +
                "    ext357             String,\n" +
                "    ext358             String,\n" +
                "    ext359             String,\n" +
                "    ext360             String,\n" +
                "    ext361             String,\n" +
                "    ext362             String,\n" +
                "    ext363             String,\n" +
                "    ext364             String,\n" +
                "    ext365             String,\n" +
                "    ext366             String,\n" +
                "    ext367             String,\n" +
                "    ext368             String,\n" +
                "    ext369             String,\n" +
                "    ext370             String,\n" +
                "    ext371             String,\n" +
                "    ext372             String,\n" +
                "    ext373             String,\n" +
                "    ext374             String,\n" +
                "    ext375             String,\n" +
                "    ext376             String,\n" +
                "    ext377             String,\n" +
                "    ext378             String,\n" +
                "    ext379             String,\n" +
                "    ext380             String,\n" +
                "    ext381             String,\n" +
                "    ext382             String,\n" +
                "    ext383             String,\n" +
                "    ext384             String,\n" +
                "    ext385             String,\n" +
                "    ext386             String,\n" +
                "    ext387             String,\n" +
                "    ext388             String,\n" +
                "    ext389             String,\n" +
                "    ext390             String,\n" +
                "    ext391             String,\n" +
                "    ext392             String,\n" +
                "    ext393             String,\n" +
                "    ext394             String,\n" +
                "    ext395             String,\n" +
                "    ext396             String,\n" +
                "    ext397             String,\n" +
                "    ext398             String,\n" +
                "    ext399             String,\n" +
                "    ext400             String,\n" +
                "    ext401             String,\n" +
                "    ext402             String,\n" +
                "    ext403             String,\n" +
                "    ext404             String,\n" +
                "    ext405             String,\n" +
                "    ext406             String,\n" +
                "    ext407             String,\n" +
                "    ext408             String,\n" +
                "    ext409             String,\n" +
                "    ext410             String,\n" +
                "    ext411             String,\n" +
                "    ext412             String,\n" +
                "    ext413             String,\n" +
                "    ext414             String,\n" +
                "    ext415             String,\n" +
                "    ext416             String,\n" +
                "    ext417             String,\n" +
                "    ext418             String,\n" +
                "    ext419             String,\n" +
                "    ext420             String,\n" +
                "    ext421             String,\n" +
                "    ext422             String,\n" +
                "    ext423             String,\n" +
                "    ext424             String,\n" +
                "    ext425             String,\n" +
                "    ext426             String,\n" +
                "    ext427             String,\n" +
                "    ext428             String,\n" +
                "    ext429             String,\n" +
                "    ext430             String,\n" +
                "    ext431             String,\n" +
                "    ext432             String,\n" +
                "    ext433             String,\n" +
                "    ext434             String,\n" +
                "    ext435             String,\n" +
                "    ext436             String,\n" +
                "    ext437             String,\n" +
                "    ext438             String,\n" +
                "    ext439             String,\n" +
                "    ext440             String,\n" +
                "    ext441             String,\n" +
                "    ext442             String,\n" +
                "    ext443             String,\n" +
                "    ext444             String,\n" +
                "    ext445             String,\n" +
                "    ext446             String,\n" +
                "    ext447             String,\n" +
                "    ext448             String,\n" +
                "    ext449             String,\n" +
                "    ext450             String,\n" +
                "    ext451             String,\n" +
                "    ext452             String,\n" +
                "    ext453             String,\n" +
                "    ext454             String,\n" +
                "    ext455             String,\n" +
                "    ext456             String,\n" +
                "    ext457             String,\n" +
                "    ext458             String,\n" +
                "    ext459             String,\n" +
                "    ext460             String,\n" +
                "    ext461             String,\n" +
                "    ext462             String,\n" +
                "    ext463             String,\n" +
                "    ext464             String,\n" +
                "    ext465             String,\n" +
                "    ext466             String,\n" +
                "    ext467             String,\n" +
                "    ext468             String,\n" +
                "    ext469             String,\n" +
                "    ext470             String,\n" +
                "    ext471             String,\n" +
                "    ext472             String,\n" +
                "    ext473             String,\n" +
                "    ext474             String,\n" +
                "    ext475             String,\n" +
                "    ext476             String,\n" +
                "    ext477             String,\n" +
                "    ext478             String,\n" +
                "    ext479             String,\n" +
                "    ext480             String,\n" +
                "    ext481             String,\n" +
                "    ext482             String,\n" +
                "    ext483             String,\n" +
                "    ext484             String,\n" +
                "    ext485             String,\n" +
                "    ext486             String,\n" +
                "    ext487             String,\n" +
                "    ext488             String,\n" +
                "    ext489             String,\n" +
                "    ext490             String,\n" +
                "    ext491             String,\n" +
                "    ext492             String,\n" +
                "    ext493             String,\n" +
                "    ext494             String,\n" +
                "    ext495             String,\n" +
                "    ext496             String,\n" +
                "    ext497             String,\n" +
                "    ext498             String,\n" +
                "    ext499             String,\n" +
                "    ext500             String" +
                ")   engine = Distributed(ck_cluster,default,behavior_log_pri2_test,hiveHash(userid));";

        for (String url : urls) {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(userName);
            dataSource.setPassword(password);
            dataSource.setTestWhileIdle(true);
            dataSource.setMaxActive(100);
            dataSource.setMaxWait(60000);
            dataSource.setTestOnBorrow(false);
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            jdbcTemplate.execute(localTableSql);
        }

        for (String url : distributeUrl) {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            dataSource.setUsername(userName);
            dataSource.setPassword(password);
            dataSource.setTestWhileIdle(true);
            dataSource.setMaxActive(100);
            dataSource.setMaxWait(60000);
            dataSource.setTestOnBorrow(false);
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            jdbcTemplate.execute(distributeTableSql);
        }


    }
}
